package com.artpri.service.impl;

import java.io.File;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.artpri.service.EmailService;

/**
 * @author ZhangSi
 */
public class EmailServiceImpl implements EmailService {

	private JavaMailSenderImpl senderImpl;
	
	public void setSenderImpl(JavaMailSenderImpl senderImpl) {
		this.senderImpl = senderImpl;
	}

	@Override
	public void htmlSend(String sendTo, String activateLink, String userName) throws Exception{
		senderImpl = new JavaMailSenderImpl();  
		  
        // 设定mail server  
        senderImpl.setHost("smtp.qq.com");  
  
        MimeMessage mailMessage = senderImpl.createMimeMessage();  
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");  
  
        // 设置收件人，寄件人  
        messageHelper.setTo(sendTo);  
        messageHelper.setFrom("1569808335@qq.com");  
        messageHelper.setSubject("中华精品网-账号激活");  
        
        // true 表示启动HTML格式的邮件  
        messageHelper.setText(  
        		"<!Doctype html> <html lang='en'> <head> <meta charset='UTF-8'> <title>Document</title> </head> <body> <table style='margin-left:auto; margin-right:auto; font-size:13px;' align='center' border='0' cellpadding='0' cellspacing='0' width='530'> <tbody><tr> <td> <table style='border-bottom:1px solid #C9E7FF;' border='0' cellpadding='0' cellspacing='0' width='530'> <tbody> <tr> <td style='padding:17px 0 13px 16px;'> <a href='http://localhost:8080/jpw/indexPicture' target='_blank'> <img src=\"cid:logo\" border='0' height='57' width='179'> </a> </td> <td style='padding-bottom:2px;' align='right' valign='bottom'> <a href='http://localhost:8080/jpw/indexPicture' target='_blank' style='color:#008efe; font-size:13px; text-decoration:none; padding-bottom:15px;'>进入中华精品网</a> </td> </tr> </tbody></table> <table style=' margin-left:50px;' border='0' cellpadding='0' cellspacing='0' width='430'> <tbody> <tr> <td style='padding:29px 0 25px 0; font-size:13px;'> 亲爱的<span style='color:#008EFE;'>"+userName+": </span></td> </tr> <tr> <td style='padding-bottom:18px; line-height:1.5em; font-size:13px;'>欢迎加入中华精品网！<br> 请点击下面链接，完成注册。 </td> </tr> <tr> <td style='padding-bottom:15px; line-height:1.6em; font-size:13px;word-break:break-all'> <a href='"+activateLink+"' target='_blank' style='color:#0036D7;'>"+activateLink+"</a><br> 如果以上链接无法点击，请将上述地址复制到您的浏览器地址栏。</td> </tr> <tr> <td style='color:#888888; font-size:13px;'>(本email为精品网系统自动发送，请勿回复)</td> </tr> <tr> <td style='padding-top:80px; padding-bottom:100px;color:#a9a9a9; font-size:13px;' align='right'>中华精品网<br> <a href='http://localhost:8080/jpw/indexPicture' target='_blank' style='color:#999; text-decoration:none'>http://localhost:8080/jpw/indexPicture</a></td> </tr> </tbody> </table> <table style='border-top:1px solid #C9E7FF;' border='0' cellpadding='0' cellspacing='0' width='530'> <tbody> <tr> <td style='padding:21px 0 70px 0; text-align:center; font-size:13px;'>Copyright Reserved <span style='border-bottom:1px dashed #ccc;z-index:1' t='7' onclick='return false;' data='2015-2020'>2015-2020</span> 中华精品网 版权所有</td> </tr> </tbody> </table></td> </tr> </tbody></table> </body> </html>"
        		, true);  
  
        FileSystemResource img = new FileSystemResource(new File("D:\\jpw\\logo.png"));  
  
        messageHelper.addInline("logo", img);  
  
        senderImpl.setUsername("1569808335@qq.com"); 
        senderImpl.setPassword("tpkysznieujahfei"); 
        
        // 设置参数
        Properties prop = new Properties();  
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.timeout", "25000");  
        senderImpl.setJavaMailProperties(prop);  
  
        // 发送邮件  
        senderImpl.send(mailMessage);  
  
        System.out.println("邮件发送成功.."); 
	}

}
